
package.path = '/usr/local/KKCJ_LUA_NEW/gcsfun/?.lua;'.. package.path
--package.cpath = '/usr/lib/?.so;'.. package.cpath
--package.path = '/Users/hongliang/Documents/LuaCode/KKCJ_LUA_NEW/gcsfun/?.lua;'.. package.path
package.cpath = '/usr/lib/?.so;'.. package.cpath

local utils = require "utils"
local zmq = require "lzmq"
local cjson = require "cjson"
local db_utils = utils.db
local config_utils = utils.config
local tb_config = config_utils.read('./gcs_config.conf')
local tb_nodes = {}
local tb_zmq = {}                ----连接zmq服务器节点的对象 {[node]={ip=,skt=,node=}},[node]={skt=}}

local mysql_db,err = db_utils:_mysql_new(tb_config)     ----初始化mysql对象
if not mysql_db then
    print("mysql 对象初始化失败,err:"..err)
    os.exit()
end


local query = "select * from nodes"
local res,err = mysql_db:query(query)
if not res then
    print("mysql_db query err,err:"..err)
end

local ctx = zmq.context()
tb_nodes = res
-------初始化tb_zmq
for i,v in pairs(tb_nodes) do
    local node = v.node
    local skt,err = ctx:socket{zmq.REQ,
    linger = 0, rcvtimeo = tb_config.zmq_timeout;
    connect = "tcp://"..node
    }
    
    tb_zmq[node] = skt
end

---准备发送配置信息
for i,v in pairs(tb_nodes) do
    local node = v.node
    local skt = tb_zmq[node]
    local tb_send_data = {}
    local tb_config = {}
    tb_config.log_level = v.log_level
    tb_config.log_path = v.log_path
    tb_config.log_rotatemode = v.log_rotatemode
    tb_config.log_rotatesize = v.log_rotatesize
    tb_config.url = v.url
    tb_config.curl_timeout = v.curl_timeout
    tb_send_data.cmd = "config"
    tb_send_data.content = tb_config
    local send_data = cjson.encode(tb_send_data)
    skt:send(send_data)
    local res_data,err = skt:recv()
    print("发送节点:",node)
    print("发送命令,内容:",send_data)
    print("收到内容:",res_data)
    
end

